package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.constant.SeatTypeEnum;
import com.example.dto.SoldCountDTO;
import com.example.mapper.EventMapper;
import com.example.mapper.SeatMapper;
import com.example.mapper.StatisticsMapper;
import com.example.mapper.TicketMapper;
import com.example.pojo.Event;
import com.example.pojo.Seat;
import com.example.pojo.Ticket;
import com.example.service.StatisticsService;
import com.example.vo.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Log4j2
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private TicketMapper ticketMapper;
    @Override
    public SoldCountVO getByEventID(Integer eventID) {
        Map<String, Object> map = new HashMap<>();
        map.put("eventID",eventID);
        statisticsMapper.getByEventID(map);
        SoldCountDTO soldCountDTO = JSON.parseObject(JSON.toJSONString(map), SoldCountDTO.class);
        SoldCountVO soldCountVO = new SoldCountVO();
        BeanUtils.copyProperties(soldCountDTO, soldCountVO);
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        //钱
        soldCountVO.setTotalPrice(decimalFormat.format(soldCountDTO.getTotalPrice()));
        soldCountVO.setLowTotalPrice(decimalFormat.format(soldCountDTO.getLowTotalPrice()));
        soldCountVO.setMidTotalPrice(decimalFormat.format(soldCountDTO.getMidTotalPrice()));
        soldCountVO.setHighTotalPrice(decimalFormat.format(soldCountDTO.getHighTotalPrice()));
        //rate
        soldCountVO.setTotalPriceRate(String.format("%.3f", soldCountDTO.getTotalPriceRate()));
        soldCountVO.setMidTotalPriceRate(String.format("%.3f", soldCountDTO.getMidTotalPriceRate()));
        soldCountVO.setHighTotalPriceRate(String.format("%.3f", soldCountDTO.getHighTotalPriceRate()));
        soldCountVO.setLowTotalPriceRate(String.format("%.3f", soldCountDTO.getLowTotalPriceRate()));
        return soldCountVO;
    }

    @Override
    public GraphVO weekList(Integer eventID) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        GraphVO graphVO = new GraphVO();
        //给 TicketRecordVO中的xdataList赋值
        List<String> xdataList = new ArrayList<>();
        for (int i=6;i>=0;i--)
        {
            LocalDate localDate = date.minusDays(i);
            String s = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            xdataList.add(s);
        }
        graphVO.setXdata(xdataList);

        //设置legendData字段
        List<String> legendDataList = new ArrayList<>();
        legendDataList.add("低档票");
        legendDataList.add("中档票");
        legendDataList.add("高档票");
        graphVO.setLegendData(legendDataList);

        //统计最近7天卖的各个档次的票
        //低等座
        List<Map<String,Object>> lowTicketList = ticketMapper.brokenLineGraphList(eventID,1);
        Integer[] lowTicket = {0,0,0,0,0,0,0};
        for (Map<String, Object> stringObjectMap : lowTicketList) {
            LocalDate ticketDate = LocalDate.parse(stringObjectMap.get("days").toString(),fmt);
            int ticketCount =Integer.valueOf(stringObjectMap.get("count").toString());
            //得到两个日期相差多少天
            int i = (int) ticketDate.until(date, ChronoUnit.DAYS);
            stringObjectMap.get("days");
            lowTicket[6-i] = ticketCount;
        }
        List<Integer> lowWeekTicket = Arrays.asList(lowTicket);
        //中等座
        List<Map<String,Object>> midTicketList = ticketMapper.brokenLineGraphList(eventID,2);
        Integer[] midTicket = {0,0,0,0,0,0,0};
        for (Map<String, Object> stringObjectMap : midTicketList) {
            LocalDate ticketDate = LocalDate.parse(stringObjectMap.get("days").toString(),fmt);
            int ticketCount =Integer.valueOf(stringObjectMap.get("count").toString());
            //得到两个日期相差多少天
            int i = (int) ticketDate.until(date, ChronoUnit.DAYS);
            stringObjectMap.get("days");
            midTicket[6-i] = ticketCount;
        }
        List<Integer> midWeekTicket = Arrays.asList(midTicket);
        //高档座
        List<Map<String,Object>> highTicketList = ticketMapper.brokenLineGraphList(eventID,3);
        Integer[] highTicket = {0,0,0,0,0,0,0};
        for (Map<String, Object> stringObjectMap : highTicketList) {
            LocalDate ticketDate = LocalDate.parse(stringObjectMap.get("days").toString(),fmt);
            int ticketCount =Integer.valueOf(stringObjectMap.get("count").toString());
            //得到两个日期相差多少天
            int i = (int) ticketDate.until(date, ChronoUnit.DAYS);
            stringObjectMap.get("days");
            highTicket[6-i] = ticketCount;
        }
        List<Integer> highWeekTicket = Arrays.asList(highTicket);
        //封装series
        List<TicketRecordVO> seriesList = new ArrayList<>();

        //低
        TicketRecordVO lowTicketRecordVO = new TicketRecordVO();
        lowTicketRecordVO.setName("低档票");
        lowTicketRecordVO.setType("line");
        lowTicketRecordVO.setStack("Total");
        lowTicketRecordVO.setData(lowWeekTicket);
        seriesList.add(lowTicketRecordVO);
        //中
        TicketRecordVO midTicketRecordVO = new TicketRecordVO();
        midTicketRecordVO.setName("中档票");
        midTicketRecordVO.setType("line");
        midTicketRecordVO.setStack("Total");
        midTicketRecordVO.setData(midWeekTicket);
        seriesList.add(midTicketRecordVO);
        //高
        TicketRecordVO highTicketRecordVO = new TicketRecordVO();
        highTicketRecordVO.setName("高档票");
        highTicketRecordVO.setType("line");
        highTicketRecordVO.setStack("Total");
        highTicketRecordVO.setData(highWeekTicket);
        seriesList.add(highTicketRecordVO);

        graphVO.setSeries(seriesList);
        return graphVO;
    }
}
